<template>
	<view>
		<headerTar title="插座详情" />
		<view class="ScanCodeDetail" v-if="prePageObj!=''&&prePageObj.online!=0">
			<view class="top">
				<view class="t_txt1">{{prePageObj.deviceName}}</view>
				<view class="t_txt2">{{prePageObj.address}}</view>
				<view class="tip">
					<view class="kefu">
						<u-icon name="server-man" size="50rpx"></u-icon>
					</view>客服
				</view>
			</view>
			<view class="title">
				选择插座
			</view>
			<view class="bottom">
				<view :class="{'b_box0':item.portStatus==0,'b_box1':item.portStatus==1,'b_box2':item.portStatus==2}"
					@click="goPay(item)" v-for="(item,i) in tabelData" :key="i">
					<view class="num">{{item.portNo}}</view>
					<view class="b_tip">{{handleType(item.portStatus)}}</view>
				</view>
			</view>
		</view>
		<view class="onScanCodeDetail" v-else>
			设备不存在或不在线~
		</view>
	</view>
</template>

<script>
	import headerTar from '@/components/headerTar/header'
	import {
		getPortListApi
	} from '@/api/map.js'
	import {
		login
	} from '@/api/my.js'
	import {
		getDeviceByDeviceNoAPi
	} from '@/api/pay.js'
	export default {
		components: {
			headerTar
		},
		data() {
			return {
				tabelData: [],
				prePageObj: '',
				deviceId: '',
			}
		},
		onLoad: async function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log(option); //打印出上个页面传递的参数。
			//外部微信扫码 要先登录
			const q = decodeURIComponent(option.q) // 获取到二维码原始链接内容
			console.log(q, '===========q'); //打印出上个页面传递的参数。
			if (!q || q == 'undefined') {
				//手动输入设备号
				if (option.deviceNo) {
					let res = await getDeviceByDeviceNoAPi({
						deviceNo: JSON.parse(option.deviceNo)
					})
					this.prePageObj = res.data
					this.deviceId = res.data.deviceId
				} else {
					console.log('===小程序内部跳转参数');
					this.prePageObj = JSON.parse(option.deviceId)
					this.deviceId = this.prePageObj.deviceId
					console.log(this.prePageObj);
				}
			} else {
				console.log('===外部扫码');
				await this.login()
				let urlList = q.split('/')
				let res = await getDeviceByDeviceNoAPi({
					deviceNo: urlList[urlList
						.length - 1]
				})
				this.prePageObj = res.data
				this.deviceId = res.data.deviceId
			}
			getPortListApi({
				deviceId: this.deviceId
			}).then(res => {
				this.tabelData = res.data
			})
		},
		methods: {
			goPay(val) {
				console.log(val, '====端口参数');
				if (val.portStatus != 0) {
					this.$toast.info('请选择其它端口！')
					return
				}
				uni.navigateTo({
					url: '/pages/pay/pay?deviceId=' + this.deviceId + '&portId=' + val.portId
				});
			},
			handleType(val) {
				switch (val) {
					case 0:
						return '空闲'
					case 1:
						return '占用'
					case 2:
						return '异常'
				}
			},
			login() {
				// 获取登录凭证
				uni.login({
					provider: 'weixin',
					success: async (res1) => {
						console.log(res1);
						await login({
							code: res1.code
						}).then(res2 => {
							console.log(res2);
							uni.setStorageSync(
								"token",
								res2.token);
						})
					},
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.onScanCodeDetail {
		margin-top: 60rpx;
		text-align: center;
		color: #a8a8a8;
	}

	.ScanCodeDetail {
		margin: 30rpx;
		box-sizing: border-box;

		.top {
			position: relative;
			height: 120rpx;
			padding: 40rpx 30rpx;
			border-radius: 20rpx;
			border: 2rpx solid #e5e5e5;

			.t_txt1 {
				font-size: 36rpx;
				font-weight: bold;
			}

			.t_txt2 {
				width: 540rpx;
				margin-top: 30rpx;
				font-size: 28rpx;
				color: #a8a8a8;
			}

			.tip {
				position: absolute;
				right: 40rpx;
				top: 50rpx;
				font-size: 28rpx;
			}

			.kefu {
				font-size: 28rpx;
			}
		}

		.title {
			height: 120rpx;
			line-height: 120rpx;
			font-size: 40rpx;
			font-weight: bold;
			text-align: center;
		}

		.bottom {

			.b_box0,
			.b_box1,
			.b_box2 {
				width: 125rpx;
				height: 140rpx;
				line-height: 50rpx;
				margin-right: 12rpx;
				margin-bottom: 20rpx;
				display: inline-block;
				text-align: center;
				color: #4dc999;
				border-radius: 6rpx;
				border: 2rpx solid #98dfb1;

				.num {
					font-weight: bold;
					margin-top: 16rpx;
				}
			}

			.b_box1 {
				color: #ffaa00;
				background-color: #fff1d5;
				border: 2rpx solid #ffaa00;
			}

			.b_box2 {
				color: #dc0000;
				background-color: #ff6a45;
				border: 2rpx solid #dc0000;
			}

			.b_box0:nth-child(5n) {
				margin-right: 0rpx;
			}

			.b_box1:nth-child(5n) {
				margin-right: 0rpx;
			}

			.b_box2:nth-child(5n) {
				margin-right: 0rpx;
			}
		}
	}
</style>